package com.ly.mpayment.persist.service.task;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.ele.RechargeTask;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
import org.springframework.util.CollectionUtils;


@Service
public class RechargeTaskServiceImpl extends AbstractService<RechargeTask, Serializable> implements IRechargeTaskService {

	@Override
	public Page<RechargeTask> findPageTask(Map<String, Object> parameters, int start, int pageSize) {
		return this.findPageByNamed("select r from RechargeTask r where 1 = 1 #[and r.taskCity = :taskCity]#   #[and r.state = :state]#  order by r.taskCity", parameters, start, pageSize);
	}

	@Override
	public List<RechargeTask> findTask() {
		return this.find("select r from RechargeTask r where r.state = ? ", 1);
//		return this.findUnique("select r from RechargeTask r where r.state = ?", );
	}


	@Override
	public RechargeTask findTaskCity(String taskCity) {
		List<RechargeTask> tasks = this.find("select r from RechargeTask r where r.state = ? and (r.taskCity = ?  or r.taskCity = '0')  order by r.taskCity desc", 1,taskCity);
		if(CollectionUtils.isEmpty(tasks)){
			return null;
		}
		return tasks.get(0);
	}

	@Override
	public boolean saveTask(RechargeTask task) {
		if(task != null){
			this.saveOrUpdate(task);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateTask(RechargeTask task) {
		if(task != null){
			this.updateEntity(task);
			return true;
		}
		return false;
	}

	@Override
	public RechargeTask findTask(String id) {
		return this.findUnique("select r from RechargeTask r where r.taskId = ?", id);
	}

	@Override
	public boolean updateTaskState(RechargeTask task) {
		if(task != null){
			this.updateEntity(task);
			return true;
		}
		return false;
	}

}
